$(document).ready(function(){
   $("#disk-usage").load("/disk_usage");
   $("#now_downloading").load("/ajax_list");
   var refreshId = setInterval(function() {
     $("#now_downloading").load("/ajax_list");
   }, 3000);
   var refreshId3 = setInterval(function() {
     $("#disk-usage").load("/disk_usage");
   }, 10000);
  $(".pauselink").click(function() {
    var src = this.href;
    $.get(src);
    $("#list").load("/listmin");
    return false;
  });
  $(".removelink").click(function() {
    var src = this.href;
    $.get(src);
    $(this).parent().parent().html(" ");
    return false;
  });
  $(".restartlink").click(function() {
    var src = this.href;
    $.get(src);
    $("#list").load("/listmin");
    return false;
  });
  $(".view_password").click(function() {
      $(this).prev().attr('type', 'text');
  });
  $(".extract").click(function() {
      var file = $(this).parent().attr('id');
      val = $("#output").html();
      $("#output").html(val + "unrar x " + file);
      $("#output").load("/extract?file="+file);
      return false;
  })
    $(".rar").draggable();

    $(".dir").droppable({
    	accept: ".rar",
    	hoverClass: 'dir-hover',
    	drop: function(ev, ui) {
    	    var file = $(ui.draggable).find('ul').attr('id');
    	    var dest = $(this).find('ul').attr('id');
    	    $('#output').html('mv ' + file + ' ' + dest);
    	    $(ui.draggable).hide();
    //    	    $("#f2").append($(ui.draggable));
    //            $("#n2").html($(ui.draggable));
//    		var h = $(ui.draggable).children().attr('href');
    //            var h2=h.split("/");
    //            $("#i2").val(h2[3]);
    //            $("#player2").html('<img src="i/a.gif" />');
    //            $("#friends2").html('');
    //            $("#f2").submit();
    	}
    });

    $(".rename").click(function() {
        el = $(this).parent().parent().children(":first-child");
        val = el.html();
        el.html('<input type="text" id="rename-fld" />');
    })

    $("#method_view").change(function() {
        if($(this).val() == 'list') {
            $("#view_folder").addClass('list_folder');
        } else {
            $("#view_folder").removeClass('list_folder');
        }
    })

    if($("#method_view").val() == 'list') {
        $("#view_folder").addClass('list_folder');
    }

   init_view_folder();
});

function init_view_folder() {
   $('.rar').contextMenu('context_rar', {
      onContextMenu: function(e) {
         el = $(e.target);
         file = $(e.target).children().html();
         return true;
      },
      bindings: {
      'rar_extract': function(t) {
         val = $("#output").html();
         $("#output").html(val + "unrar x " + file);
         $("#output").load("/extract?file="+file);
         return false;      
      },
      'rar_extractto': function(t) {
         alert('Trigger was '+t.id+'\nAction was Email');
      },
      'rar_delete': function(t) {
         if( confirm("Seguro que quieres eliminar el archivo '" + file + "'?") ) {      
            file.replace(eval("/ /g"), "\ ");
            $("#output").load("/delete?file="+file);
            $(el).hide();
         }
         return false;
      },
      'rar_rename': function(t) {
         alert('Trigger was '+t.id+'\nAction was Delete');
      }
   }
       });
       
   $('textarea').autoResize({
       // On resize:
       onResize : function() {
           $(this).css({opacity:0.8});
       },
       // After resize:
       animateCallback : function() {
           $(this).css({opacity:1});
       },
       // Quite slow animation:
       animateDuration : 300,
       // More extra space:
       extraSpace : 4
   });       
       
   $('.dir').contextMenu('context_dir', {
      onContextMenu: function(e) {
         el = $(e.target);
         file = $(e.target).children().html();
         return true;
      },
      bindings: {
      'dir_open': function(t) {
         $("#view_folder").load("/open_folder?dir="+file);
         
      },
      'dir_delete': function(t) {
         if( confirm("Seguro que quieres eliminar el directorio " + file + "?") ) {
            $("#output").load("/delete?file="+file);
            $(el).hide();
         }
         return false;
      },
      'dir_rename': function(t) {
         alert('Trigger was '+t.id+'\nAction was Delete');
      }
   }    
    });
}


